<?php

// -----------------------------------------

// Read base path from the bash config file
$_BASH_CONF = file(dirname(__FILE__) . '/config.sh');

foreach ($_BASH_CONF as $line) {
	if (substr(trim($line),0,10) == 'BASE_PATH=') {
		$pair = explode('=', trim($line), 2);
		$BASE_PATH = trim($pair[1]);
		break;
	}
}

define('BASE_PATH', $BASE_PATH);

// -----------------------------------------

$conf['php-sigkill']           = BASE_PATH . '/.napv.evaluator.signal.sigkill';
$conf['grader-time']           = BASE_PATH . '/.grader.time';
$conf['evaluator-throttle']    = 500000;

$conf['compile']['c']   = '-O2 -std=gnu99 -static -s -lm -x c';
$conf['compile']['cpp'] = '-O2 -static -s -lm -x c++';

# Startup User Score/EXP
$conf['start']['score'] = 10000;
$conf['start']['exp']   = 3;

# Check require environment

$_OS = PHP_OS;
// For testing
$_OS = 'Linux';

$error = array();

if ($_OS != 'Linux') {
	$error[] = 'The NAPV-CE need to run on Linux.';
}

if (phpversion() < 5.2) {
	$error[] = 'The NAPV-CE require PHP at least version 5.2';
}

if (count($error) > 0) {
	$text = implode('</li><li>', $error);
	echo <<<EOT
	<html><body>
		<h1>Requirement Error</h1>
		<p>
			The <em>Napv Contest Environment</em> has detected
			following error(s) with your current installation:
		</p>
		<ul>
			<li>$text</li>
		</ul>
		<hr />
		<address>Napv Contest Environment server running on {$_SERVER['HTTP_HOST']}.</address>
	</body></html>
EOT;
	die;
}

// from class.config.php, for single editing
class Config {
	private static $me;

	private $productionServers = array('161.200.155.12');
	private $localServers	   = array('localhost');

	// Standard Config Options...

	// ...For Auth Class
	public $authDomain;		 // Domain to set for the cookie
	public $authSalt;		   // Can be any random string of characters
	public $useHashedPasswords; // Store hashed passwords in database? (versus plain-text)

	// ...For Database Class
	public $dbHost;	   // Database server
	public $dbName;	   // Database name
	public $dbUsername;   // Database username
	public $dbPassword;   // Database password
	public $dbDieOnError; // What do do on a database error (see class.database.php for details)

	// Add your config options here...
	public $useDBSessions; // Set to true to store sessions in the database

	// Singleton constructor
	private function __construct()
	{
		$this->everywhere();

		$i_am_here = $this->whereAmI();

		if('production' == $i_am_here)
			$this->production();
		elseif('local' == $i_am_here)
			$this->local();
		elseif('shell' == $i_am_here)
			$this->shell();
		else
			die('<h1>Where am I?</h1> <p>You need to setup your server names in <code>class.config.php</code></p>
				 <p><code>$_SERVER[\'HTTP_HOST\']</code> reported <code>' . $_SERVER['HTTP_HOST'] . '</code></p>');
	}
	
	public static function getConfig()
	{
		if(is_null(self::$me))
			self::$me = new Config();
		return self::$me;
	}

	// Allow access to config settings statically.
	// Ex: Config::get('some_value')
	public static function get($key)
	{
		return self::$me->$key;
	}

	// Add code to be run on all servers
	private function everywhere()
	{
		// Store sesions in the database?
		$this->useDBSessions = !true;

		// Settings for the Auth class
		$this->authDomain		 = $_SERVER['HTTP_HOST'];
		$this->useHashedPasswords = false;
		$this->authSalt		   = ''; // Pick any random string of characters
	}

	// Add code/variables to be run only on production servers
	private function production()
	{
		ini_set('display_errors', '0');

		define('WEB_ROOT', 'http://' . $_SERVER['HTTP_HOST']);

		$this->dbHost	    = 'localhost';
		$this->dbName	    = 'napv-ce';
		$this->dbUsername   = 'root';
		$this->dbPassword   = '3738';
		$this->dbDieOnError = false;
	}

	// Add code/variables to be run only on local (testing) servers
	private function local()
	{
		ini_set('display_errors', '1');
		ini_set('error_reporting', E_ALL);

		if (PHP_OS == 'Linux')
			define('WEB_ROOT', 'http://' . $_SERVER['HTTP_HOST']);
		else
			define('WEB_ROOT', 'http://' . $_SERVER['HTTP_HOST'] . '/pws/ce/htdocs');

		$this->dbHost	   = 'localhost';
		$this->dbName	   = 'napv-ce';
		$this->dbUsername   = 'root';
		$this->dbPassword   = '';
		$this->dbDieOnError = true;
		if (PHP_OS == 'Linux')
			$this->dbPassword   = '3738';
	}

	// Add code/variables to be run only on when script is launched from the shell
	private function shell()
	{
		ini_set('display_errors', '1');
		ini_set('error_reporting', E_ALL);

		define('WEB_ROOT', '');

		$this->dbHost	   = 'localhost';
		$this->dbName	   = 'napv-ce';
		$this->dbUsername   = 'root';
		$this->dbPassword   = '3738';
		$this->dbDieOnError = true;
	}

	public function whereAmI()
	{
		if(in_array($_SERVER['HTTP_HOST'], $this->productionServers))
			return 'production';
		elseif(in_array($_SERVER['HTTP_HOST'], $this->localServers))
			return 'local';
		elseif(isset($_ENV['SHELL']))
			return 'shell';
		else
			return false;
	}
}
